package com.qqtech.qquan.mall.dao;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;

@Repository
public class MallShopDaoImpl extends BaseDaoImpl<MallShop> implements MallShopDao {
	public static final Logger log = LoggerFactory.getLogger(MallShopDaoImpl.class);

	@Override
	public Page<MallShopVo> queryPageListAdmin(MallShop query,
			Pageable pageable) {
		List<MallShopVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		return new PageImpl<MallShopVo>(contentList, pageable, this.queryCount(query));
	}

	@Override
	public int addSaleOrderCount(Integer shopId) {
		if (shopId == null) {
			return 0;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", shopId);
		return sqlSessionTemplate.update(getSqlName("addSaleOrderCount"), map);
	}

	@Override
	public int subSaleOrderCount(Integer shopId) {
		if (shopId == null) {
			return 0;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", shopId);
		return sqlSessionTemplate.update(getSqlName("subSaleOrderCount"), map);
	}

	@Override
	public List<MallShopVo> queryListByCollect(Integer userId, Integer communityId, Integer type, Integer style, Integer auditStatus) {
		if (userId == null) {
			return null;
		}
		MallShopQuery q = new MallShopQuery();
		q.setCreatorId(userId); //收藏店铺的用户
		q.setAuditStatus(auditStatus);
		q.setType(type);
		q.setStyle(style);
		q.setCommunityId(communityId);
		return sqlSessionTemplate.selectList(getSqlName("selectByCollect"), BeanUtil.toMap(q));
	}

	@Override
	public List<MallShopVo> queryAllShop() {
		return sqlSessionTemplate.selectList(getSqlName("selectAll"));
	}
	
	@Override
	public Page<MallShopVo> queryDistancePage(MallShopQuery query, Pageable pageable) {
		List<MallShopVo> contentList = this.sqlSessionTemplate.selectList(getSqlName("selectDistance"),
				getParams(query, pageable));
		return new PageImpl<MallShopVo>(contentList, pageable, queryDistanceCount(query));
	}

	public int queryDistanceCount(MallShopQuery query) {
		return ((Integer) this.sqlSessionTemplate.selectOne(getSqlName("selectDistanceCount"), BeanUtil.toMap(query)))
				.intValue();
	}

	@Override
	public List<MallShopVo> queryListByCollectV1(Integer userId, Integer communityId, Integer type, Integer style,
			Integer auditStatus, BigDecimal x, BigDecimal y) {
		if (userId == null) {
			return null;
		}
		MallShopQuery q = new MallShopQuery();
		q.setCreatorId(userId); //收藏店铺的用户
		q.setAuditStatus(auditStatus);
		q.setType(type);
		q.setStyle(style);
		q.setCommunityId(communityId);
		q.setX(x);
		q.setY(y);
		return sqlSessionTemplate.selectList(getSqlName("selectByCollect"), BeanUtil.toMap(q));
	}
}
